package com.imay.live.activity;

import android.content.Context;
import android.content.Intent;
import android.support.v4.app.Fragment;
import android.support.v4.view.ViewPager;
import android.os.Bundle;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.TypeReference;
import com.imay.base.activity.BaseActivity;
import com.imay.base.util.ViewUtil;
import com.imay.live.fragment.PermissionCheckFragment;
import com.imay.live.model.PermissionModel;
import com.imay.live.proto.Result;
import com.imay.live.proto.helper.ProtoCallBack;
import com.imay.live.proto.helper.ProtoHttpFactory;
import com.imay.live.proto.helper.ProtoRequestHelper;
import com.imay.live.view.indicator.ScrollIndicatorView;
import com.imay.live.R;
import com.imay.live.adapter.IndicatorFragmentAdapter;
import com.imay.live.model.TabIndicatorModel;
import com.imay.live.view.indicator.IndicatorViewPager;
import com.imay.live.view.indicator.slidebar.ColorBar;
import com.imay.live.view.indicator.transition.OnTransitionTextListener;

import java.util.ArrayList;
import java.util.List;

import static com.imay.live.proto.ResultCode.CodeSuccess;

/**
 * 我的->设置->查看特权
 */
public class PermisionCheckActivity extends BaseActivity implements ProtoCallBack {


    private ScrollIndicatorView view_Indicator;
    private ViewPager widget_viewpager;
    private List mTabModelList;
    private String loadLivePermission;

    public static void start(Context context) {
        Intent starter = new Intent(context, PermisionCheckActivity.class);
//        starter.putExtra();
        context.startActivity(starter);
    }
    @Override
    protected int getLayoutId() {
        return R.layout.activity_permision_check;
    }

    @Override
    protected void initView(Bundle savedInstanceState) {
        view_Indicator=$(R.id.notice_siv);
        widget_viewpager=$(R.id.notice_viewpager);
        setTitle(getString(R.string.setting_permision_check_title));
        loadData();

    }

    private void bindFragment(PermissionModel model) {

        mTabModelList = new ArrayList<>(2);
        mTabModelList.add(new TabIndicatorModel(getString(R.string.permision_user),false));
        mTabModelList.add(new TabIndicatorModel(getString(R.string.permision_anchor),false));

        List<Fragment> fragmentList = new ArrayList<>(2);
        fragmentList.add(PermissionCheckFragment.newInstance(model,PermissionCheckFragment.TYPE_USER));
        fragmentList.add(PermissionCheckFragment.newInstance(model,PermissionCheckFragment.TYPE_LIVE));
        widget_viewpager.setOffscreenPageLimit(1);
        ColorBar colorBar =  new ColorBar(this, getResources().getColor(R.color.indicator_bar_select), getResources().getDimensionPixelOffset(R.dimen.indicator_height));
        colorBar.setWidth((int) ViewUtil.dip2px(this,25f));
        view_Indicator.setScrollBar(colorBar);
        view_Indicator.setOnTransitionListener(new OnTransitionTextListener().setColorId(this, R.color.indicator_text_select, R.color.indicator_text_unselect));
        IndicatorViewPager indicatorViewPager = new IndicatorViewPager(view_Indicator, widget_viewpager);
        indicatorViewPager.setAdapter(new IndicatorFragmentAdapter(this,getSupportFragmentManager(),fragmentList,mTabModelList) {
        });
    }

    private void loadData(){
        loadLivePermission = ProtoRequestHelper.task(ProtoHttpFactory.getPermission()).setTag(this.hashCode()).request(this);

    }

    @Override
    public void onRequestSuccess(String command, String data) {
        if (command.equals(loadLivePermission)){
            Result<PermissionModel> modelResult = JSON.parseObject(data,new TypeReference<Result<PermissionModel>>(){}.getType());
            if (modelResult.result==CodeSuccess){
                bindFragment(modelResult.data);
            }
        }

    }

    @Override
    public void onRequestError(String command, int code, String message) {

    }
}
